package cn.exrick.xboot.modules.commonlyUsed.controller;

import cn.exrick.xboot.common.utils.PageUtil;
import cn.exrick.xboot.common.utils.ResultUtil;
import cn.exrick.xboot.common.vo.PageVo;
import cn.exrick.xboot.common.vo.Result;
import cn.exrick.xboot.modules.commonlyUsed.dto.CommonlyDTO;
import cn.exrick.xboot.modules.commonlyUsed.entity.CommonlyUsed;
import cn.exrick.xboot.modules.commonlyUsed.service.ICommonlyUsedService;
import cn.hutool.core.lang.UUID;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author XBW
 */
@Slf4j
@RestController
@Api(description = "常用功能管理接口")
@RequestMapping("/xboot/commonlyUsed")
@Transactional
public class CommonlyUsedController {

    @Autowired
    private ICommonlyUsedService iCommonlyUsedService;

    @RequestMapping(value = "/get/{id}", method = RequestMethod.GET)
    @ApiOperation(value = "通过id获取常用功能注册详细信息")
    public Result<CommonlyUsed> get(@PathVariable String id){

        CommonlyUsed commonlyUsed = iCommonlyUsedService.getById(id);
        return new ResultUtil<CommonlyUsed>().setData(commonlyUsed);
    }

    @RequestMapping(value = "/getAll", method = RequestMethod.GET)
    @ApiOperation(value = "获取常用功能注册的全部数据")
    public Result<List<CommonlyUsed>> getAll(CommonlyDTO commonlyDTO){

        List<CommonlyUsed> list = iCommonlyUsedService.commonlyList(commonlyDTO);
        return new ResultUtil<List<CommonlyUsed>>().setData(list);
    }

    @RequestMapping(value = "/insertOrUpdate", method = RequestMethod.POST)
    @ApiOperation(value = "编辑或更新数据常用功能注册")
    public Result<CommonlyUsed> saveOrUpdate(@ModelAttribute CommonlyUsed commonlyUsed){

        if(iCommonlyUsedService.saveOrUpdate(commonlyUsed)){
            commonlyUsed.setId(UUID.randomUUID().toString());
            return new ResultUtil<CommonlyUsed>().setData(commonlyUsed);
        }
        return new ResultUtil<CommonlyUsed>().setErrorMsg("操作失败");
    }

    @RequestMapping(value = "/delByIds/{id}", method = RequestMethod.DELETE)
    @ApiOperation(value = "批量通过id删除公司下启用的功能")
    public Result<Object> delAllByIds(@PathVariable String id){

        iCommonlyUsedService.deleteCommonly(id);

        return new ResultUtil<Object>().setSuccessMsg("删除数据成功");
    }
}
